export const BVID_SUBTITLE_MAP_TONGYI_CHAT_ID = 'bvid_subtitle_map_tongyi_chat_id'
export const BVID_COMMENTS_MAP_TONGYI_CHAT_ID = 'bvid_comments_map_tongyi_chat_id'
import _ from 'lodash'

const createChat = async (content: string, prompt: string) => {

    const cookieKeyValue: any = {
        'tongyi_sso_ticket': undefined,
        'UM_distinctid': undefined,
        'cna': undefined,
        'xlly_s': undefined,
        'isg': undefined,
        'tfstk': undefined,
    }

    // 获取 example.com 的所有 cookie（不限制是否为当前标签页）
    const cookies: any = await chrome.cookies.getAll({ url: "https://www.tongyi.com/" });

    _.keys(cookieKeyValue).forEach((name) => {
        const value = _.find(cookies, { name })?.value
        if (_.isNil(value)) {
            delete cookieKeyValue[name]
        } else {
            cookieKeyValue[name] = value
        }
    })


    let cookie = _.keys(cookieKeyValue).map((key) => `${key}=${cookieKeyValue[key]}`).join('; ')

    return await fetch("https://cili-server.vercel.app/proxy", {
        method: "POST",
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify({
            "headers": {
                "accept": "text/event-stream",
                "accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-US;q=0.6",
                "cache-control": "no-cache",
                "content-type": "application/json",
                "pragma": "no-cache",
                "priority": "u=1, i",
                "sec-ch-ua": "\"Not;A=Brand\";v=\"99\", \"Google Chrome\";v=\"139\", \"Chromium\";v=\"139\"",
                "sec-ch-ua-mobile": "?0",
                "sec-ch-ua-platform": "\"macOS\"",
                "sec-fetch-dest": "empty",
                "sec-fetch-mode": "cors",
                "sec-fetch-site": "same-site",
                "x-platform": "pc_tongyi",
                "x-xsrf-token": "70a97642-4155-424f-b57e-c5d44f9cfd89",
                "cookie": cookie,

                "Referer": "https://www.tongyi.com/qianwen"
            },
            "data": {
                "model": "",
                "action": "next",
                "mode": "chat",
                "userAction": "new_top",
                "requestId": "07ad86cfbec149059a3f8baf2f196088",
                "sessionId": "",
                "sessionType": "text_chat",
                "parentMsgId": "",
                "params": {
                    "agentId": "",
                    "searchType": "",
                    "pptGenerate": false,
                    "bizScene": "",
                    "bizSceneInfo": {},
                    "specifiedModel": "tongyi-qwen3-max-model",
                    "deepThink": false,
                    "deepResearch": false
                },
                "contents": [
                    {
                        "content": `${prompt}\n${content}`,
                        "contentType": "text",
                        "role": "user",
                        "ext": {
                            "searchType": "",
                            "pptGenerate": false,
                            "deepThink": false,
                            "deepResearch": false
                        }
                    }
                ]
            },
            "method": "POST",
            "targetUrl": "https://api.tongyi.com/dialog/conversation"
        }),
        redirect: "follow"
    });
}

export default {
    createChat
}
